package com.zhongfl.crm.service.impl.boss;

import com.zhongfl.crm.lixiao.mapper.BankMapper;
import com.zhongfl.crm.service.boss.BankService;
import com.zhongfl.crm.model.boss.Bank;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tech.heptagram.channel.api.common.AssertUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * BankServiceImpl 银行配置 服务实现类
 *
 * @author tiantian
 * @date 2018/8/9 18:36
 */
@Service
public class BankServiceImpl implements BankService {
    @Resource
    private BankMapper bankMapper;

    @Override
    public void save(Bank bank) {
        AssertUtils.notNull(bank, "新增记录为空！");
        if("BRANCH".equals(bank.getBankType())){
            setBankIcon(bank);
        }
        bankMapper.insertSelective(bank);
    }

    private void setBankIcon(Bank bank){
        Bank parent = bankMapper.selectById(bank.getParentId());
        if(StringUtils.isNotEmpty(parent.getBankIcon())){
            bank.setBankIcon(parent.getBankIcon());
        }
    }

    @Override
    public void deleteById(Integer id, String staffName) {
        AssertUtils.notNull(id, "记录ID为空！");
        AssertUtils.hasText(staffName, "操作人为空！");
        bankMapper.deleteById(id, staffName);
    }

    @Override
    public void updateById(Bank bank) {
        AssertUtils.notNull(bank, "更新内容为空！");
        AssertUtils.notNull(bank.getId(), "记录ID为空！");
        bankMapper.updateByIdSelective(bank);

        updateBranchBankIcon(bank);
    }

    private void updateBranchBankIcon(Bank bank) {
        Bank parent = bankMapper.selectById(bank.getId());
        if(StringUtils.isNotEmpty(bank.getBankIcon()) && !bank.getBankIcon().equals(parent.getBankIcon())){
            List<Bank> bankList = bankMapper.selectBranchBanksByParentId(bank.getId());
            if(CollectionUtils.isNotEmpty(bankList)){
                bankList.forEach(item->{
                    Bank temp = new Bank();
                    temp.setId(item.getId());
                    temp.setBankIcon(bank.getBankIcon());
                    bankMapper.updateByIdSelective(bank);
                });
            }
        }
    }

    @Override
    public Bank findById(Integer id) {
        AssertUtils.notNull(id, "记录ID为空！");
        return bankMapper.selectById(id);
    }

    @Override
    public List<Bank> findMasterBanks() {
        return bankMapper.selectMasterBanks();
    }

    @Override
    public List<Bank> findBranchBanks() {
        return bankMapper.selectBranchBanks();
    }

    @Override
    public List<Bank> findBranchBanksByParentId(Integer parentId) {
        return bankMapper.selectBranchBanksByParentId(parentId);
    }

    @Override
    public Bank findByIdAndParentId(Integer bankId, Integer parentId) {
        AssertUtils.notNull(bankId,"银行ID为空！");
        AssertUtils.notNull(parentId,"银行父级ID为空！");
        return bankMapper.selectByIdAndParentId(bankId,parentId);
    }
}
